package cn.stylefeng.guns.config.redis.redis;


import cn.stylefeng.guns.config.redis.GlobalJedisConnectionFactory;
import cn.stylefeng.guns.config.redis.GlobalPoolConfig;
import cn.stylefeng.guns.config.redis.GlobalRedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * Created by L on 2017/3/1.
 */
@Component
public class Redis0Template extends RedisTemplate {

    @Autowired
    private GlobalRedisConfig globalRedisConfig;

    @Autowired
    GlobalPoolConfig globalPoolConfig;

    @PostConstruct
    public void init(){
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        setKeySerializer(stringSerializer);
        setValueSerializer(stringSerializer);
        setHashKeySerializer(stringSerializer);
        setHashValueSerializer(stringSerializer);

//        GlobalPoolConfig globalPoolConfig = new GlobalPoolConfig();
//        globalPoolConfig.setMaxTotal(globalRedisConfig.getMaxActive());
//        globalPoolConfig.setMaxIdle(globalRedisConfig.getMaxIdle());
//        globalPoolConfig.setMaxWaitMillis(globalRedisConfig.getMaxWait());
//        globalPoolConfig.setMinEvictableIdleTimeMillis(globalRedisConfig.getMinTimeMillis());
//        globalPoolConfig.setMinIdle(globalRedisConfig.getMinIdle());
//        globalPoolConfig.setNumTestsPerEvictionRun(globalRedisConfig.getNumTestsPerEvictionRun());
//        globalPoolConfig.setTimeBetweenEvictionRunsMillis(globalRedisConfig.getTimeBetweenEvictionRunsMillis());

        GlobalJedisConnectionFactory globalJedisConnectionFactory = new GlobalJedisConnectionFactory();
        globalJedisConnectionFactory.setHostName(globalRedisConfig.getHostName());
        globalJedisConnectionFactory.setPort(globalRedisConfig.getRedisPort());
        globalJedisConnectionFactory.setPassword(globalRedisConfig.getRedisPasswod());
        globalJedisConnectionFactory.setDatabase(globalRedisConfig.getDataBase0());
        globalJedisConnectionFactory.setPoolConfig(globalPoolConfig);
        globalJedisConnectionFactory.afterPropertiesSet();
        setConnectionFactory(globalJedisConnectionFactory);

    }

}
